package form.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	
	import form.ui.component.Layout.Box;
	import form.ui.core.FMSprite;
	import form.ui.core.IDirectionAble;

	public class ApplicationContainer extends FMSprite implements IDirectionAble
	{
		public static var application:ApplicationContainer;
		
		public var layout:Box;
		private var overlay:Sprite;
		private var bubbleLayout:Sprite;
		
		public function ApplicationContainer()
		{
			super();		
			
			bubbleLayout = new Sprite();
			i_addChild(bubbleLayout);
			
			layout = new Box("vertical");
			i_addChild(layout);
			
			overlay = new Sprite();
			i_addChild(overlay);
			
			this.registerStyle("background");
			this.registerStyle("backgroundSize");
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage, false, 0, true);
			application = this;
		}
		
		private function i_addChild(child:DisplayObject):DisplayObject
		{
			return super.addChild(child);
		}
		
		private function i_addChildAt(child:DisplayObject, index:uint):DisplayObject
		{
			return super.addChildAt(child, index);
		}
		
		private function onAddToStage(e:Event):void
		{
			onResize();
			this.stage.addEventListener(Event.RESIZE, onResize);
		}
		
		private function onResize(e:Event = null):void
		{
			this.width = this.stage.stageWidth;
			this.height = this.stage.stageHeight;
		}
		
		public function get direction():String
		{
			return layout.direction;
		}
		
		public function set direction(value:String):void
		{
			if(direction != value)
			{
				layout.direction = value;
			}
		}
		
		public function get verticalGap():Number
		{
			return layout.verticalGap;layout.verticalAlign
		}		
		public function set verticalGap(value:Number):void
		{
			layout.verticalGap = value;
		}
		
		public function get verticalAlign():String
		{
			return layout.verticalAlign;
		}		
		public function set verticalAlign(value:String):void
		{
			layout.verticalAlign = value;
		}
		
		public function get horizontalGap():Number
		{
			return layout.horizontalGap;
		}
		public function set horizontalGap(value:Number):void
		{
			layout.horizontalGap = value;
		}
		
		public function get horizontalAlign():String
		{
			return layout.horizontalAlign;
		}		
		public function set horizontalAlign(value:String):void
		{
			layout.horizontalAlign = value;
		}
		
		override public function set width(value:Number):void
		{
			if(value > 0)
			{
				super.width = value;
				layout.width = value;
			}
			
		}
		
		override public function set height(value:Number):void
		{
			if(value > 0)
			{
				super.height = value;
				layout.height = value;
			}
			
		}
		
		override public function set percentWidth(value:Number):void
		{
			
			
		}
		
		override public function set percentHeight(value:Number):void
		{
			
		}
		
		override public function get paddingLeft():Number
		{
			return layout.paddingLeft;
		}
		override public function set paddingLeft(value:Number):void
		{
			layout.paddingLeft = value;
		}
		
		override public function get paddingRight():Number
		{
			return layout.paddingRight;
		}
		override public function set paddingRight(value:Number):void
		{
			layout.paddingRight = value;
		}
		
		override public function get paddingTop():Number
		{
			return layout.paddingTop;
		}
		override public function set paddingTop(value:Number):void
		{
			layout.paddingTop = value;
		}
		
		override public function get paddingBottom():Number
		{
			return layout.paddingBottom;
		}
		override public function set paddingBottom(value:Number):void
		{
			layout.paddingBottom = value;
		}
		
		public function set background(value:String):void
		{
			layout.background = value;
		}
		
		public function set backgroundSize(value:Number):void
		{
			layout.backgroundSize = value;
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(child is FMSprite) return layout.addChild(child);
			else
			{
				throw new Error("Only frame displayobject can be added to layout.");
			}
			return null;
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if(child is FMSprite) return layout.addChildAt(child, index);
			else
			{
				throw new Error("Only frame displayobject can be added to layout.");
			}
			return null;
		}
		
		public function addBubbleDisplay(child:DisplayObject):DisplayObject
		{
			return bubbleLayout.addChild(child);
		}
		
		public function removeBubbleDisplay(child:DisplayObject):DisplayObject
		{
			return bubbleLayout.removeChild(child);
		}
		
		public function clearBubbleDisplay():void
		{
			while(bubbleLayout.numChildren > 0)
			{
				bubbleLayout.removeChildAt(0);
			}
		}
		
		private var graphicMode:Boolean;
		public function addOverlay(child:DisplayObject, blue:Boolean = false, mode:Boolean = false):DisplayObject
		{
			if(mode)
			{
				graphicMode = true;
				deawOverlay();
			}
			if(blue)
			{
				layout.filters = [new BlurFilter(5, 5)];
			}
			return overlay.addChild(child);
		}
		
		public function removeOverlay(child:DisplayObject):DisplayObject
		{
			overlay.graphics.clear();
			layout.filters = [];
			graphicMode = false;
			if(overlay.contains(child))
			{
				return overlay.removeChild(child)
			}
			return null;
		}
		
		public function hasOverlay(child:DisplayObject):Boolean
		{
			return overlay.contains(child);
		}
		
		public function clearOverlay():void
		{
			overlay.graphics.clear();
			layout.filters = [];
			graphicMode = false;
			while(overlay.numChildren > 0)
			{
				overlay.removeChildAt(0);
			}
		}
		
		private function deawOverlay():void
		{
			if(graphicMode && overlay)
			{
				overlay.graphics.clear();
				overlay.graphics.beginFill(0, 0.5);
				overlay.graphics.drawRect(0,0,this.width, this.height);
				overlay.graphics.endFill();
			}
		}
		
		override public function draw():void
		{
			super.draw();
			deawOverlay();
		}
	}
}